<template>
  <div class="login">
    <div class="login-wrap">
      <el-row type="flex" justify="center">
        <el-form ref="loginForm" :model="user" :rules="rules" status-icon label-width="80px" class="form-css">
          <hr>
          <el-form-item prop="username" label="用户名">
            <el-input v-model="user.username" placeholder="请输入用户名" prefix-icon></el-input>
          </el-form-item>
          <el-form-item id="password" prop="password" label="密码">
            <el-input v-model="user.password" show-password placeholder="请输入密码"></el-input>
          </el-form-item>
          <router-link to="/register">注册账号</router-link>
          <el-form-item>
            <el-button type="primary" icon="el-icon-upload" @click="submitForm">登 录</el-button>
          </el-form-item>
        </el-form>
      </el-row>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login";

export default {
  name: "login",
  data() {
    return {
      user: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 3, max: 12, message: "密码长度应在 6 到 12 个字符之间", trigger: "blur" },
        ],
      },
      redirect: "/index", // 登录成功后重定向的路径
    };
  },
  methods: {
    submitForm() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          const formDate = {
            username :this.user.username,
            password :this.user.password,
          };
          login(formDate)
              .then(res => {console.log(res);
                if (res.code === 200) {

                  sessionStorage.setItem("user", res.data.user); // 使用 this.user.username
                  sessionStorage.setItem("token", res.data.token);
                  this.$router.replace("/home");
                  this.$message.success("登录成功");
                } else {
                  this.$message.error("登录失败111");
                }
              })
              .catch(error => {
                console.error("登录请求失败:", error);
                this.$message.error("登录请求失败，请重试！");
              });
        } else {
          this.$message.error("请检查表单中的错误！");
        }
      });
    },
  },
};
</script>

<style scoped>
.login {
  background: url("../assets/bg/bg1.png") no-repeat;
  background-size: 102% 130%;
  overflow: hidden;
}
.login-wrap {
  background: url("../assets/bg/from.png") no-repeat;
  background-size: 100% 100%;
  width: 700px;
  height: 500px;
  border-radius: 0 0 2px 2px;
  margin: 215px auto;
  overflow: hidden;
  padding-top: 10px;
  line-height: 40px;
  box-shadow: 0 4px 15px rgba(255, 165, 100, 0.7); /* 添加边框阴影 */
}
.form-css {
  margin-top: 130px;
  margin-left: 230px;
}
#password {
  margin-bottom: 5px;
}
hr {
  background-color: #444;
  margin: 20px auto;
}
a {
  text-decoration: none;
  color: #aaa;
  font-size: 15px;
}
a:hover {
  color: coral;
}
.el-button {
  width: 100%;
  margin-left: -50px;
  background: orange;
}
</style>
